<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="loading" />
    <!-- /加载中 loading -->

    <div class="error" v-if="is404">
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ articleData.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="articleData.aut_photo"
        />
        <div class="text">
          <p class="name">{{ articleData.aut_name }}</p>
          <p class="time">{{ articleData.pubdate | relativeTime }}</p>
        </div>
        <van-button
          round
          :color="articleData.is_followed ? 'rgb(127 135 236 )' : '#3296fa'"
          size="small"
          type="info"
          @click="hFollowed(articleData.is_followed, articleData.aut_id)"
          >{{ articleData.is_followed ? "已关注" : "+ 关注" }}</van-button
        >
      </div>
      <div class="content">
        <div v-html="articleData.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          @click="hLikeToggle"
          hairline
          type="primary"
          plain
          :icon="articleData.attitude === 1 ? 'good-job' : 'good-job-o'"
        >
          {{ articleData.attitude === 1 ? "取消点赞" : "点赞" }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          @click="hDisLikeToggle"
          hairline
          type="danger"
          plain
          icon="delete"
          >
           {{ articleData.attitude === 0 ? "取消不喜欢" : "不喜欢" }}
          </van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <article-comment :articleId="articleId"></article-comment>
    <!-- 文章评论 -->

  </div>
</template>
<script>
import { getArticle, addLike, deleteLike, addDisLike, deleteDisLike } from '@/api/article.js'
import { followUser, unFollowUser } from '@/api/user.js'
import ArticleComment from './comment.vue'
export default {
  name: 'ArticleIndex',
  components: { ArticleComment },
  data () {
    return {
      is404: true,
      loading: true, // 控制加载中的 loading 状态
      articleData: {},
      articleId: ''
    }
  },
  mounted () {
    this.mGetArticleData()
    this.articleId = this.$route.params.id
  },
  methods: {
    async mGetArticleData () {
      this.is404 = false // ----------------------------------------- 1.初始化为非404状态
      this.loading = true // ---------------------------------------- 2.加载读条start
      const data = await getArticle({ id: this.$route.params.id }) // 3.发送请求获取文章数据
      if (!data) this.is404 = true // ------------------------------- 4.如果读取内容为空触发404
      this.loading = false // --------------------------------------- 5.加载读条end
      this.articleData = data.data // ------------------------------- 6.接收数据
    },
    async hFollowed (state, target) { // 关注与取关
      try {
        // ↓ state为 true 进行取关  为 false 进行关注
        state ? await unFollowUser(target) : await followUser({ target }) // 1.判断状态进行-关注/取关请求
        this.articleData.is_followed = !this.articleData.is_followed// ----- 2.更新本地
      } catch (err) {
        console.log('关注/取关失败', err)
      }
    },
    async hLikeToggle () { // 点赞/取消点赞
      const target = this.articleData.art_id.toString()
      try {
        // ↓ 1点赞状态  -1无状态
        this.articleData.attitude === 1 // 1.判断是否已点赞
          ? await deleteLike(target) // -- 2.发送取消点赞请求
          : await addLike({ target })// --   or发送点赞请求
        this.articleData.attitude *= -1 // 3.更新本地状态(1和-1切换)
        this.$toast.success('操作成功')
      } catch (err) {
        console.log('点赞操作失败')
      }
    },
    async hDisLikeToggle () { // 不喜欢/喜欢切换
      const target = this.articleData.art_id.toString()
      try {
        // ↓ 状态0为不喜欢 -1无状态
        if (this.articleData.attitude === 0) { // 1.判断是否已喜欢
          await deleteDisLike(target) // -------- 2.(1)取消喜欢
          this.articleData.attitude = -1 // ----- 2.(2)更新本地状态
        } else {
          await addDisLike({ target }) // ------- 2.(1)添加喜欢
          this.articleData.attitude = 0 // ------ 2.(2)更新本地状态
          this.$toast.success('操作成功') // ----- 3.发出提示
        }
      } catch (err) {
        console.log('点赞操作失败')
      }
    }
  }

}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
